<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Collect references to completed tasks</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.worker.html">« Worker</a></li>
      <li style="float: right;"><a href="worker.getstacked.html">Worker::getStacked »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.worker.html">Worker</a></li>
    <li>Collect references to completed tasks</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="worker.collect" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Worker::collect</h1>
  <p class="verinfo">(PECL pthreads &gt;= 3.0.0)</p><p class="refpurpose"><span class="refname">Worker::collect</span> &mdash; <span class="dc-title">Collect references to completed tasks</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-worker.collect-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Worker::collect</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type Callable">Callable</a></span> <code class="parameter">$collector</code><span class="initializer"> = ?</span></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   Allows the worker to collect references determined to be garbage by the
   optionally given collector.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-worker.collect-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">collector</code></dt>

    <dd>

     <p class="para">
      A Callable collector that returns a boolean on whether the task can be
      collected or not. Only in rare cases should a custom collector need to
      be used.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-worker.collect-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   The number of remaining tasks on the worker&#039;s stack to be collected.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-worker.collect-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3336">
    <p><strong>示例 #1 A basic example of <span class="methodname"><strong>Worker::collect()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$worker&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Worker</span><span style="color: #007700">();<br /><br />echo&nbsp;</span><span style="color: #DD0000">"There&nbsp;are&nbsp;currently&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collect</span><span style="color: #007700">()}</span><span style="color: #DD0000">&nbsp;tasks&nbsp;on&nbsp;the&nbsp;stack&nbsp;to&nbsp;be&nbsp;collected\n"</span><span style="color: #007700">;<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">15</span><span style="color: #007700">;&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">stack</span><span style="color: #007700">(new&nbsp;class&nbsp;extends&nbsp;</span><span style="color: #0000BB">Threaded&nbsp;</span><span style="color: #007700">{});<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"There&nbsp;are&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collect</span><span style="color: #007700">()}</span><span style="color: #DD0000">&nbsp;tasks&nbsp;remaining&nbsp;on&nbsp;the&nbsp;stack&nbsp;to&nbsp;be&nbsp;collected\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">start</span><span style="color: #007700">();<br /><br />while&nbsp;(</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collect</span><span style="color: #007700">());&nbsp;</span><span style="color: #FF8000">//&nbsp;blocks&nbsp;until&nbsp;all&nbsp;tasks&nbsp;have&nbsp;finished&nbsp;executing<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"There&nbsp;are&nbsp;now&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">collect</span><span style="color: #007700">()}</span><span style="color: #DD0000">&nbsp;tasks&nbsp;on&nbsp;the&nbsp;stack&nbsp;to&nbsp;be&nbsp;collected\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$worker</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">shutdown</span><span style="color: #007700">();</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
There are currently 0 tasks on the stack to be collected
There are 15 tasks remaining on the stack to be collected
There are now 0 tasks on the stack to be collected
</pre></div>
    </div>
   </div>
  </p>
 </div>

</div></div></div></body></html>